<?php

use Illuminate\Database\Seeder;
use App\User;
use App\Model\Article;

class FavouriteTableSeeder extends Seeder
{
    const TABLE = 'favourite';
    /**
     * php artisan make:seeder FavouriteTableSeeder
     *
     * php artisan db:seed --class=FavouriteTableSeeder
     *
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table(static::TABLE)->truncate();

        $data = [];
        $users = User::all()->pluck('id')->toArray();
        $cases = Article::all()->pluck('id')->toArray();
        shuffle($cases);

        foreach ($users as $user) {
            shuffle($cases);
            $num = rand(0, 5);

            $favourites = array_slice($cases, 0, $num);
            foreach ($favourites as $favourite) {
                $data[] = [
                    'user_id' => $user,
                    'article_id' => $favourite,
                    'is_active' => rand(0, 1),
                ];
            }
        }

        DB::table(static::TABLE)->insert($data);

    }
}
